package core.manager.battle
{
	import core.manager.battle.events.BattleEvent;
	
	import flash.events.EventDispatcher;
	
	import mapengine.avatars.IGameAvatar;
	import mapengine.interfaces.IMapAvatar;
	
	import utils.AvatarValueUtil;
	
	import values.avatar.AvatarRoundState;
	
	import view.component.message.Message;

	public class SingleBattleManager extends EventDispatcher implements IBattleManager
	{
		private var _currentRound:int = 0;

		public function get currentRound():int
		{
			return _currentRound;
		}

		public function set currentRound(value:int):void
		{
			_currentRound = value;
			
			BattleEvent.dispatchEvent(BattleEvent.ROUND_CHANGE , value);
		}
		
		private var _A:Array;
		
		private var _B:Array;
		
		private var _sence:String;
		
		private var firstTurn:int = BattleCampType.A_SIDE;
		
		public function init(A:Array , B:Array , sence:String , isMonster:Boolean = true):void
		{
			_A = A;
			_B = B;
			_sence = sence;
			if(isMonster)
			{
				firstTurn = BattleCampType.A_SIDE;
			}
			else
			{
				firstTurn = AvatarValueUtil.countActRate(A) > AvatarValueUtil.countActRate(B) ? BattleCampType.A_SIDE : BattleCampType.B_SIDE;
			}
		}
		
		public function deleteAvatar(value:IGameAvatar):void
		{
			if(_A.indexOf(value) != -1)
			{
				_A.splice(_A.indexOf(value));
			}
			else
			{
				if(_B.indexOf(value) != -1)
				{
					_B.splice(_B.indexOf(value));
				}
			}
		}
		
		public function updateRound():void
		{
			currentAvatar = null;
			_lastAvatar = null;
			if(_turn != firstTurn)
			{
				currentRound++;
				Message.show("第" + currentRound + "回合!!");
			}
			
			var v:IGameAvatar;
			if(_turn == -1)
			{
				whosTurn = firstTurn;
			}
			else
			{
				whosTurn = (_turn == BattleCampType.A_SIDE ? BattleCampType.B_SIDE : BattleCampType.A_SIDE);
			}
			
			if(whosTurn == 0)
			{
				for each(v in _B)
				{
					v.avatarValue.currentRoundState = AvatarRoundState.NOT_MY_TURN;
				}
				
				for each(v in _A)
				{
					v.avatarValue.currentRoundState = AvatarRoundState.MOVE;
				}
			}
			else
			{
				for each(v in _B)
				{
					v.avatarValue.currentRoundState = AvatarRoundState.MOVE;
				}
				
				for each(v in _A)
				{
					v.avatarValue.currentRoundState = AvatarRoundState.NOT_MY_TURN;
				}
			}
			
			///预防某方的单位某个回合全部不能移动的囧壮
			roundEnd();
		}
		
		public function dispose():void
		{
			
		}
		
		private var _isAuto:Boolean = false;
		public function get isAuto():Boolean
		{
			return _isAuto;
		}
		
		public function set isAuto(value:Boolean):void
		{
			_isAuto = value;
		}
		
		public function endNow():void
		{
			
		}
		
		public function roundEnd():void
		{
			var arr:Array;
			if(_turn == BattleCampType.A_SIDE) arr = _A;
			else arr = _B;
			
			var isRoundEnd:Boolean = true;
			
			for each(var v:IGameAvatar in arr)
			{
				if(v.avatarValue.currentRoundState != AvatarRoundState.FINISHED)
				{
					isRoundEnd = false;
					if(isAuto || arr == _B)
					{
						currentAvatar = v;
					}
					break;
				}
			}
			
			if(isRoundEnd)
			{
				updateRound();
			}
		}
		
		public function getAvatarsBySide(value:int):Array
		{
			return (value != -1 ? _A : _B);
		}
		
		private var _currentAvatar:IGameAvatar;
		
		public function set currentAvatar(value:IGameAvatar):void
		{
			if(_currentAvatar != null) 
			_lastAvatar = _currentAvatar;
			_currentAvatar = value;
			
			if(isAuto || _turn == 1)
			{
				if(_currentAvatar != null)
				{
					_currentAvatar.myRound(createAI(_currentAvatar));
				}
			}
			
			BattleEvent.dispatchEvent(BattleEvent.AVATAR_CHANGE , value);
		}
		
		private var _lastAvatar:IGameAvatar;
		
		public function createAI(value:IGameAvatar):Array
		{
//			if(value.avatarValue.ultimateSkill && value.avatarValue.ultimateSkill.canUseSkill())
//			{
//				
//			}
			
			return BattleAIManager.checkNormalStep(value);
			
			//return null;
		}
		
		public function get currentAvatar():IGameAvatar
		{
			return _currentAvatar;
		}
		
		private var _turn:int = -1;
		
		public function set whosTurn(value:int):void
		{
			_turn = value;
			
			BattleEvent.dispatchEvent(BattleEvent.TURN_CHANGE , _turn);
		}
		
		public function get whosTurn():int
		{
			return _turn;
		}
	}
}